package com.gzs.zhdj.controller;

import com.gzs.zhdj.domain.dto.ActPageDto;
import com.gzs.zhdj.domain.dto.ActivitiesDto;
import com.gzs.zhdj.domain.dto.PageBean;
import com.gzs.zhdj.domain.dto.ParticipantDto;
import com.gzs.zhdj.service.ActivityService;
import com.gzs.zhdj.utils.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/zhdj")
@Slf4j
public class ActivityController {
    @Autowired
    ActivityService activityService;
    @PostMapping("/admin/createact")
    public CommonResponse createAct(@RequestBody ActivitiesDto act){
        log.info("活动信息: {}", act);
        activityService.createAct(act);
        return CommonResponse.ok();
    }
    @PostMapping("/partymember/joinact")
    public CommonResponse joinAct(int id){
        log.info("活动名: {}", id);
        activityService.joinAct(id);
        return CommonResponse.ok();
    }
    @PostMapping("/partymember/cancelact")
    public CommonResponse cancelAct(int id){
        log.info("活动名: {}", id);
        activityService.cancelAct(id);
        return CommonResponse.ok();
    }
    @GetMapping("/getallact")
    public CommonResponse getAllAct(@RequestParam(required = false) String name,
                                    @RequestParam(required = false)
                                    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") LocalDateTime start,
                                    @RequestParam(required = false)
                                    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")LocalDateTime end,
                                    @RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int pageSize){
        log.info("活动分页查询参数：name={}, start={}, end={}, page={}, pageSize={}", name, start, end, page, pageSize);
        PageBean actPage = activityService.getAct(name,start,end,page,pageSize);
        return CommonResponse.ok(actPage);
    }
    @GetMapping("/getactbyid")
    public CommonResponse getActById(int id){
        log.info("id :{}", id);
        ActivitiesDto activities = activityService.getActById(id);
        System.out.println(activities);
        return CommonResponse.ok(activities);
    }
    @GetMapping("/partymember/getact")
    public CommonResponse getAct(@RequestParam(required = false) String name,
                                 @RequestParam(required = false)
                                 @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")LocalDateTime start,
                                 @RequestParam(required = false)
                                 @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")LocalDateTime end,
                                 @RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "10") int pageSize) {
        // 打印请求参数
        log.info("活动分页查询参数：name={}, start={}, end={}, page={}, pageSize={}", name, start, end, page, pageSize);
        // 调用业务层
        PageBean actPage = activityService.getSelfAct(name,start,end,page,pageSize);
        System.out.println(actPage);
        // 返回响应
        return CommonResponse.ok(actPage);
    }
    @PutMapping("/updateact")
    public CommonResponse updateAct(@RequestBody ActivitiesDto act){
        log.info("活动信息: {}", act);
        activityService.updateAct(act);
        return CommonResponse.ok();
    }
    @PutMapping("/partymember/uploadfeel")
    public CommonResponse uploadFeel(@RequestParam int id, MultipartFile file){
        activityService.uploadFeel(id,file);
        return CommonResponse.ok();
    }
    @GetMapping("/getfeel")
    public CommonResponse getFeel(@RequestParam int id){
        List<ParticipantDto> participantDtoList = activityService.getFeel(id);
        return CommonResponse.ok(participantDtoList);
    }
    @GetMapping("getselffeel")
    public CommonResponse getSelfFeel(@RequestParam int id){
        List<ParticipantDto> participantDtoList = activityService.getSelfFeeel(id);
        return CommonResponse.ok(participantDtoList);
    }
    @DeleteMapping("admin/deleteact")
    public CommonResponse deleteact(int id){
        activityService.deleteAct(id);
        return CommonResponse.ok();
    }
}
